package com.anrongtec.zcpt.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.anrongtec.zcpt.ApiConfig;
import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.base.BaseActivity;
import com.anrongtec.zcpt.fragment.AddressBookFragment;
import com.anrongtec.zcpt.fragment.MainFragment;
import com.anrongtec.zcpt.fragment.StoreFragment;
import com.anrongtec.zcpt.fragment.TrendsFragment;
import com.anrongtec.zcpt.fragment.WeChatFragment;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.main_frame_layout)
    FrameLayout mainFrameLayout;
    @BindView(R.id.ll_homepage)
    LinearLayout llHomepage;
    @BindView(R.id.ll_we_chat)
    LinearLayout llWeChat;
    @BindView(R.id.ll_address_book)
    LinearLayout llAddressBook;
    @BindView(R.id.ll_trends)
    LinearLayout llTrends;
    @BindView(R.id.ll_shopping)
    LinearLayout llShopping;
    @BindView(R.id.iv_main)
    ImageView ivMain;//首页图片
    @BindView(R.id.iv_message)
    ImageView ivMessage;//会话图片
    @BindView(R.id.iv_address_book)
    ImageView ivAddressBook;//通讯录图片
    @BindView(R.id.iv_trends)
    ImageView ivTrends;//动态图片
    @BindView(R.id.iv_shopping)
    ImageView ivShopping;//商店图片
    //五个fragment
    private MainFragment mf;//首页fragment
    private WeChatFragment wcf;//绘话fragment
    private AddressBookFragment abf;//通讯录fragment
    private TrendsFragment tf;//动态fragment
    private StoreFragment sf;//优警微店fragment

    private static final String[] FRAGMENT_TAG = {"mf","wcf","abf","tf","sf"};
    private static final String PRV_SELINDEX="PREV_SELINDEX";
    private int selindex=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleShow(false);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initTopBar(this,false,"移动警务","");
        //第一次初始化首页默认显示第一个fragment
        FragmentManager sfm = getSupportFragmentManager();
        if (savedInstanceState != null) {
        //读取上一次界面Save的时候tab选中的状态
            selindex=savedInstanceState.getInt(PRV_SELINDEX,selindex);
            mf = (MainFragment) sfm.findFragmentByTag(FRAGMENT_TAG[0]);
            wcf = (WeChatFragment) sfm.findFragmentByTag(FRAGMENT_TAG[1]);
            abf = (AddressBookFragment) sfm.findFragmentByTag(FRAGMENT_TAG[2]);
            tf = (TrendsFragment) sfm.findFragmentByTag(FRAGMENT_TAG[3]);
            sf = (StoreFragment) sfm.findFragmentByTag(FRAGMENT_TAG[4]);

        }
        select(selindex);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(PRV_SELINDEX,selindex);
        super.onSaveInstanceState(outState);
    }

    private  void  select(int i){
          //开启事务，fragment的控制是由事务来实现的
       FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //隐藏所有fragment
        hideFragment(transaction);
          switch (i){
              case 0:
                  //初始化fragment并添加到事务中，如果为null就new一个
                  if (mf == null) {
                      mf = new MainFragment("");
                      transaction.add(R.id.main_frame_layout, mf,FRAGMENT_TAG[0]);
                  } else {
                      //显示需要显示的fragment
                      transaction.show(mf);
                  }
                  break;
              case 1:
                  if (wcf==null){
                      wcf=new WeChatFragment("此服务暂未开通");
                      transaction.add(R.id.main_frame_layout,wcf,FRAGMENT_TAG[1]);
                  } else {
                      transaction.show(wcf);
                  }
                  break;
              case 2:
                  if(abf==null){
                      abf=new AddressBookFragment("此服务暂未开通");
                      transaction.add(R.id.main_frame_layout,abf,FRAGMENT_TAG[2]);
                  }else {
                      transaction.show(abf);
                  }
                  break;
              case 3:
                  if (tf==null){
                      tf=new TrendsFragment("此服务暂未开通");
                      transaction.add(R.id.main_frame_layout,tf,FRAGMENT_TAG[3]);
                  }else {
                      transaction.show(tf);
                  }
                  break;
              case 4:
                  if (sf==null){
                      sf=new StoreFragment();
                      transaction.add(R.id.main_frame_layout,sf,FRAGMENT_TAG[4]);
                  }else {
                      transaction.show(sf);
                  }
                  break;
          }
          transaction.commit();
      }
    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (mf != null) {
            transaction.hide(mf);
        }
        if (wcf != null) {
            transaction.hide(wcf);
        }
        if (abf != null) {
            transaction.hide(abf);
        }
        if (tf != null) {
            transaction.hide(tf);
        }
        if (sf != null) {
            transaction.hide(sf);
        }
    }

    @OnClick({R.id.ll_homepage, R.id.ll_we_chat, R.id.ll_address_book, R.id.ll_trends, R.id.ll_shopping})
    public void onClick(View view) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        switch (view.getId()) {
            case R.id.ll_homepage:
                resetImg();
                ivMain.setImageResource(R.drawable.icon_homepage_pressed);
                select(0);
                break;
            case R.id.ll_we_chat:
                resetImg();
                ivMessage.setImageResource(R.drawable.icon_wechat_pressed);
                select(1);
                break;
            case R.id.ll_address_book:
                resetImg();
                ivAddressBook.setImageResource(R.drawable.icon_address_book_pressed);
                select(2);
                break;
            case R.id.ll_trends:
                resetImg();
                ivTrends.setImageResource(R.drawable.ic_trends_pressed);
                select(3);
                break;
            case R.id.ll_shopping:
                resetImg();
                ivShopping.setImageResource(R.drawable.icon_shopping_pressed);
                select(4);
                break;
        }
        ft.commit();
    }

    private void resetImg() {
        ivMain.setImageResource(R.drawable.icon_homepage_normal);
        ivMessage.setImageResource(R.drawable.icon_wechat_normal);
        ivAddressBook.setImageResource(R.drawable.icon_address_book_normal);
        ivTrends.setImageResource(R.drawable.ic_trends_normal);
        ivShopping.setImageResource(R.drawable.icon_shopping_normal);
    }


    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click(); //调用双击退出函数
        }
        return false;
    }

    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
//            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            showToastCustom("再按一次退出程序", ApiConfig.WARNING);
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
            System.exit(0);
        }
    }
}